﻿using AutoMapper;
using Fortunate.API.BASE;
using Fortunate.IService;
using Fortunate.Common.Helper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Fortunate.Common.Enum;
using Fortunate.Common.Global;

namespace Fortunate.API.Controllers
{
    /// <summary>
    /// 登录操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [AllowAnonymous]
    public class LoginController : BaseApiController
    {
        /// <summary>
        /// 
        /// </summary>
        protected readonly ILoginService _iloginService;
        private readonly TokenHelper _tokenManager;
        /// <summary>
        /// 
        /// </summary>
        protected readonly IMapper _mapper;
        /// <summary>
        /// 
        /// </summary>
        public LoginController(ILoginService iLoginService, TokenHelper tokenManager, IMapper mapper)
        {
            _iloginService = iLoginService;
            _tokenManager = tokenManager;
            _mapper = mapper;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LoginAccount()
        {
            if (FortunateGlobalContext.isTest != null && FortunateGlobalContext.isTest == "1")
            {
                string code = MemoryCacheHelper.Get(CacheKeyEnum.FortunateCode.ToString()).ToString();
                if (string.IsNullOrEmpty(code))
                {
                    return Fail("请输入图形验证码");
                }
            }

            string account = "admin";
            string password = "123";
            string Salt = PasswordHelper.GetSalt(await _iloginService.GetAccountSalt(account));
            if (string.IsNullOrEmpty(Salt))
            {
                return Fail("账号或密码错误");
            }
            string passwordSalt = PasswordHelper.GenEncodingPassword(password, Salt);
            var _user = await _iloginService.GetLogin(account, passwordSalt);
            _user.Password = _user.Salt = "";
            if (_user != null)
            {
                return Success(data: _tokenManager.GetToken(_user), Message: "登录成功");
            }
            return Fail("账号或密码错误");
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> LoginPhone()
        {
            var userList = await _iloginService.GetLogin("16698556985");
            if (userList.Count <= 0)
            {
                return Fail("没有找到该手机绑定的账号，请检查");
            }
            if (userList.Count > 1)
            {
                return Fail("该手机号码绑定多个账号，不允许使用手机号码登录");
            }
            string token = _tokenManager.GetToken(userList.FirstOrDefault());
            return Success(data: token, Message: "登录成功");
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetCode()
        {
            string code = VerifyCodeHelper.GetCode();
            MemoryCacheHelper.Set(CacheKeyEnum.FortunateCode.ToString(), code, new TimeSpan(hours: 0, minutes: 10, seconds: 0));
            //var stream = VerifyCodeHelper.Create(code);
            //string base64 = string.Concat("data:image/png;base64,", Convert.ToBase64String(stream.ToArray()));
            var stream = VerifyCodeHelper.CreateValidGraphic(code);
            string base64 = string.Concat("data:image/png;base64,", Convert.ToBase64String(stream.ToArray()));
            stream.Dispose();
            return Success(base64);
        }



    }

}
